/*
 * Copyright 2010 Electronic Business Systems Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package gr.ebs.gssdroid.resource;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * A container for the results of a search query for users.
 *
 * @author past
 */
public class UserSearchResource extends RestResource {

	public UserSearchResource(String aUri) {
		super(aUri);
	}

	List<UserResource> users = new ArrayList<UserResource>();

	@Override
	public void createFromJSON(String text) throws JSONException {
		JSONArray json = new JSONArray(text);
		for (int i = 0; i < json.length(); i++) {
				JSONObject j = (JSONObject) json.get(i);
				if (j != null) {
					String username = unmarshallString(j, "username");
					String name = unmarshallString(j, "name");
					String home = unmarshallString(j, "home");
					UserResource user = new UserResource(home);
					user.setName(name);
					user.setUsername(username);
					users.add(user);
				}
			}
	}

	/**
	 * Retrieve the users.
	 *
	 * @return the users
	 */
	public List<UserResource> getUsers() {
		return users;
	}

	/**
	 * Modify the users.
	 *
	 * @param newUsers the users to set
	 */
	public void setUsers(List<UserResource> newUsers) {
		users = newUsers;
	}

	@Override
	public String getLastModifiedSince() {
		return null;
	}
}
